﻿using Hr.IDAL;
using Hr.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hr.DAL
{
    public partial class ResumeListDAL
    {
        // 创建EF对象
        private DbContext db = DbContextFactory.Create();

        /// <summary>
        /// 模糊查询简历信息
        /// </summary>
        /// <returns>简历信息</returns>
        /// <author>赵匡涛</author>
        /// <update>null</update>
        public IEnumerable<object> GetResume_ListAll(string Name)
        {
            return (from ResumeList in db.Set<Resume_List>()
                    select new
                    {
                        ResumeList.pk_Resume,
                        TypeNmae_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Resume_List") & n.ID == ResumeList.TypeID).FirstOrDefault().TypeName_CHS,
                        ResumeList.FullName,
                        ImagePath=ResumeList.ImagePath.Length>0? ResumeList.ImagePath:"/",
                        ResumeList.Age,
                        AnimalSign=ResumeList.AnimalSign.Length>0? ResumeList.AnimalSign:"无",
                        ResumeList.Province,
                        ResumeList.Birthday,
                        ResumeList.Years,
                        ResumeList.StartDate,
                        ResumeList.EndDate,
                        ResumeList.Description,
                        StatusNmae_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Resume_List") & n.ID == ResumeList.StatusID).FirstOrDefault().StatusName_CHS,
                    }).Where(n => n.FullName.Contains(Name)).ToList();
        }
        /// <summary>
        /// 获取简历信息
        /// </summary>
        /// <returns>简历信息</returns>
        /// <author>赵匡涛</author>
        /// <create>2020/8/9</create>
        /// <update>null</update>
        public IEnumerable<object> GetResumeInfo()
        {

            //return db.Set<Resume_List>().ToList();
            return (from ResumeList in db.Set<Resume_List>()
                    select new
                    {
                        ResumeList.pk_Resume,
                        TypeNmae_CHS=db.Set<Sys_TypeID>().Where(n=>n.TableName.Equals("Resume_List")&n.ID==ResumeList.TypeID).FirstOrDefault().TypeName_CHS,
                        
                        ResumeList.FullName,
                        //NickName= db.Set<User_List>().SingleOrDefault(n => n.pk_User.ToString() == ResumeList.NickName).FullName,
                        CreateUserName=db.Set<User_List>().FirstOrDefault(n=>n.pk_User==ResumeList.CreateUser).FullName,
                        ResumeList.ImagePath,
                        ResumeList.Age,
                        ResumeList.Years,
                        ResumeList.AnimalSign,
                        ResumeList.Province,
                        ResumeList.Birthday,
                        ResumeList.StartDate,
                        ResumeList.EndDate,
                        ResumeList.Description,
                        StatusNmae_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Resume_List") & n.ID == ResumeList.StatusID).FirstOrDefault().StatusName_CHS,
                    }).ToList();
        }
        /// <summary>
        /// 根据id查询简历信息
        /// </summary>
        /// <returns>简历信息</returns>
        /// <author>赵匡涛</author>
        /// <create>2020/8/9</create>
        /// <update>null</update>
        public Resume_List ResumeListInfo(int ResumeID)
        {
            return db.Set<Resume_List>().SingleOrDefault(n => n.pk_Resume == ResumeID);
        }
        /// <summary>
        /// 选择器信息
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, object> TypeListANStatusList()
        {
            List<Sys_TypeID> TypeList = db.Set <Sys_TypeID>().Where(n => n.TableName.Equals("Resume_ List")).ToList();
            List<Sys_StatusID> StatusList = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Resume_ List")).ToList();
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"TypeList",TypeList },
                {"StatusList",StatusList }
            };
            return dt;
        }
        /// <summary>
        /// 删除简历信息
        /// </summary>
        /// <returns>简历信息</returns>
        /// <author>赵匡涛</author>
        /// <create>2020/8/9</create>
        /// <update>null</update>
        public void DelResume_List(Resume_List s)
        {
            db.Set<Resume_List>().Remove(s);
        }
        /// <summary>
        /// 添加 编辑
        /// </summary>
        /// <returns>简历信息</returns>
        /// <author>赵匡涛</author>
        /// <create>2020/8/9</create>
        /// <update>null</update>

        public void UpdateResume(Resume_List s, string type,int pk_User)
        {
            if (type.Equals("Add"))
            {
                //添加
                s.fk_User = pk_User;
                s.CreateDate = DateTime.Now;
                s.CreateUser = pk_User;
                s.fk_Department = db.Set<User_List>().SingleOrDefault(n => n.pk_User == pk_User).fk_Department;
                db.Set<Resume_List>().Add(s);
            }
            else if (type.Equals("Update"))
            {
                //更新操作
                //Resume_List re = db.Set<Resume_List>().SingleOrDefault(n=>n.pk_Resume == s.pk_Resume);
    
                //re.TypeID = s.TypeID;
                //re.StatusID = s.StatusID;
                //re.FullName = pk_User.ToString(); ;
                //re.Sex = s.Sex;
                //re.Age = s.Age;
                //re.Province = s.Province;
                //re.Birthday = s.Birthday;
                //re.Education = s.Education;
                //re.Language = s.Language;
                //re.Years = s.Years;
                //re.StartDate = s.StartDate;
                //re.EndDate = s.EndDate;
                //re.Remark = s.Remark;
                //re.Wechat = s.Wechat;
                //re.Comments = s.Comments;
                //re.Address = s.Address;
                //re.EndDate = s.EndDate;
                //re.Contact = s.Contact;
                //re.City = s.City;
                //re.Description = s.Description;
                //re.TEL = s.TEL;

                s.ModifyUser = pk_User;
                s.ModifyDate = DateTime.Now;
                db.Set<Resume_List>().AddOrUpdate(s);
            }
        }

        /// <summary>
        /// 获取简历姓名和用户姓名
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns>简历姓名和用户姓名</returns>
        /// <author>赵匡涛</author>
        /// <create>2020/8/7</create>
        /// <update>null</update>
        public IEnumerable<object> GetResumeName_UserName(int UserId)
        {
            //多表连接例子
            return (from ul in db.Set<User_List>()
                    join rl in db.Set<Resume_List>()
                    on ul.pk_User equals rl.fk_User
                    where ul.pk_User == UserId
                    select new { ul.UserName, rl.FullName }
                    ).ToList();

            #region 单表条件查询，例子
            // return db.Set<User_List>().Where(n => n.pk_User == id);
            #endregion
        }
        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}
